{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Checkpoints\n",
    "You can easily save and load a REBOUND simulation to a binary file. The binary file includes all information about the particles (mass, position, velocity, etc), as well as the current simulation settings such as time, integrator choice, etc.\n",
    "\n",
    "Let's add three particles to REBOUND and save them to a file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---------------------------------\n",
      "REBOUND version:     \t3.8.0\n",
      "REBOUND built on:    \tFeb  3 2019 13:37:32\n",
      "Number of particles: \t3\n",
      "Selected integrator: \twhfast\n",
      "Simulation time:     \t0.0000000000000000e+00\n",
      "Current timestep:    \t0.001000\n",
      "---------------------------------\n",
      "<rebound.Particle object, m=1.0 x=0.0 y=0.0 z=0.0 vx=0.0 vy=0.0 vz=0.0>\n",
      "<rebound.Particle object, m=1e-06 x=1.0 y=0.0 z=0.0 vx=0.0 vy=1.000000499999875 vz=0.0>\n",
      "<rebound.Particle object, m=0.0 x=2.000000999999 y=0.0 z=0.0 vx=0.0 vy=0.7071081347393496 vz=0.0>\n",
      "---------------------------------\n"
     ]
    }
   ],
   "source": [
    "import rebound\n",
    "sim = rebound.Simulation()\n",
    "sim.add(m=1.)\n",
    "sim.add(m=1e-6, a=1.)\n",
    "sim.add(a=2.)\n",
    "sim.integrator = \"whfast\"\n",
    "sim.save_to_file(\"checkpoint.bin\")\n",
    "sim.status()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The binary files are small in size and store every floating point number exactly, so you don't have to worry about efficiency or losing precision. You can make lots of checkpoints if you want!\n",
    "\n",
    "Let's delete the old REBOUND simulation (that frees up the memory from that simulation) and then read the binary file we just saved."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---------------------------------\n",
      "REBOUND version:     \t3.8.0\n",
      "REBOUND built on:    \tFeb  3 2019 13:37:32\n",
      "Number of particles: \t3\n",
      "Selected integrator: \twhfast\n",
      "Simulation time:     \t0.0000000000000000e+00\n",
      "Current timestep:    \t0.001000\n",
      "---------------------------------\n",
      "<rebound.Particle object, m=1.0 x=0.0 y=0.0 z=0.0 vx=0.0 vy=0.0 vz=0.0>\n",
      "<rebound.Particle object, m=1e-06 x=1.0 y=0.0 z=0.0 vx=0.0 vy=1.000000499999875 vz=0.0>\n",
      "<rebound.Particle object, m=0.0 x=2.000000999999 y=0.0 z=0.0 vx=0.0 vy=0.7071081347393496 vz=0.0>\n",
      "---------------------------------\n"
     ]
    }
   ],
   "source": [
    "del sim\n",
    "sim = rebound.Simulation(\"checkpoint.bin\")\n",
    "sim.status()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that you will have to re-set any function pointers manually (if you're using them)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
